//
//  AddImgView.m
//  weiGuanJia
//
//  Created by 吕飞俊 on 14-5-30.
//  Copyright (c) 2014年 吕飞俊. All rights reserved.
//

#import "AddImgView.h"
#import "lvfjCommon.h"
#import "UIView+lvfjFrame.h"
#import <MobileCoreServices/MobileCoreServices.h>

@implementation AddImgView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [self init];
    return self;
}

- (id)init{
    self = [super initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
    if (self)
    {
        _scale = 2;
        _isUserEdit = NO;
        
        self.hidden = YES;
        self.backgroundColor = [UIColor clearColor];
        
        viewBG_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        viewBG_.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        [self addSubview:viewBG_];
        
        actionSheet_ = [[UIActionSheet alloc]
                                      initWithTitle:nil
                                      delegate:self
                                      cancelButtonTitle:@"取消"
                                      destructiveButtonTitle:nil
                                      otherButtonTitles:@"从相册中获取", @"使用相机拍摄",nil];
        actionSheet_.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        
//        viewAlert_ = [[UIView alloc] initWithFrame:CGRectMake(0, DHEIGHT, DWIDTH, 145)];
//        viewAlert_.backgroundColor = [UIColor whiteColor];
//        [self addSubview:viewAlert_];
//        
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.tag = 0;
//        [btn setTitle:@"从相册中获取" forState:UIControlStateNormal];
//        [btn setBackgroundImage:[UIImage imageNamed:@"Button22_15.png"] forState:UIControlStateNormal];
//        btn.frame = CGRectMake(10, 10, DWIDTH-20, 40);
//        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [viewAlert_ addSubview:btn];
//        
//        btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.tag = 1;
//        [btn setTitle:@"使用相机拍摄" forState:UIControlStateNormal];
//        [btn setBackgroundImage:[UIImage imageNamed:@"Button22_15.png"] forState:UIControlStateNormal];
//        btn.frame = CGRectMake(10, 49, DWIDTH-20, 40);
//        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [viewAlert_ addSubview:btn];
//        
//        btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.tag = 2;
//        [btn setTitle:@"取消" forState:UIControlStateNormal];
//        [btn setBackgroundImage:[UIImage imageNamed:@"Button44_17.png"] forState:UIControlStateNormal];
//        [btn setBackgroundImage:[UIImage imageNamed:@"Button33_17.png"] forState:UIControlStateHighlighted];
//        btn.frame = CGRectMake(10, 100, DWIDTH-20, 40);
//        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        [viewAlert_ addSubview:btn];
    }
    return self;
}

- (void)show
{
    [actionSheet_ showInView:self];
//    self.height = ((UIViewController *)_delegate).view.height;
//    viewAlert_.top = ((UIViewController *)_delegate).view.height;
//
//    self.hidden = NO;
//    [UIView animateWithDuration:0.3 animations:^
//     {
//         viewAlert_.top = self.height-viewAlert_.height;
//     }];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIButton *btn = [[UIButton alloc] init];
    btn.tag = 0;
    // 从相册中获取
    if (buttonIndex == 0)
    {
        btn.tag = 0;
        [self btnClick:btn];
    }
    // 使用相机拍摄
    else if (buttonIndex == 1)
    {
        btn.tag = 1;
        [self btnClick:btn];
    }
    // 取消
    else if(buttonIndex == 2)
    {
        
    }
    
}

- (void)btnClick:(UIButton *)btn
{
    if (btn.tag == 0)
    {
        self.hidden = YES;
        viewAlert_.top = self.height;
        
        // 判断是否手机相册功能
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [imagePicker setMediaTypes:[NSArray arrayWithObject:(NSString*)kUTTypeImage]];
            [imagePicker setDelegate:self];
            imagePicker.allowsEditing = _isUserEdit;
            [((UIViewController *)_delegate) presentViewController:imagePicker animated:YES completion:nil];
        }
        else
        {
            //@"该设备不支持手机相册"
        }
    }
    // 使用相机拍摄
    else if (btn.tag == 1)
    {
        self.hidden = YES;
        viewAlert_.top = self.height;
        
        // 判断是否支持拍照功能
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
            [imagePicker setMediaTypes:[[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil]];
            [imagePicker setDelegate:self];
            imagePicker.allowsEditing = _isUserEdit;
            [((UIViewController *)_delegate) presentViewController:imagePicker animated:YES completion:nil];
        }
        else
        {
            //@"该设备不支持拍照"
        }
    }
    else
    {
        [UIView animateWithDuration:0.15 animations:^
         {
             viewAlert_.top = self.height;
         }
         completion:^(BOOL finished)
         {
             self.hidden = YES;
         }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    CGFloat TW = image.size.width;
    CGFloat TH = image.size.height;
    
    // 改变大小
    if (_scale >= 1)
    {
        if (TW > TH)
        {
            if (TW > 512)
            {
                TH = 512/TW*TH;
                TW = 512;
            }
        }
        else
        {
            if (TH > 512)
            {
                TW = 512/TH*TW;
                TH = 512;
            }
        }
    }
    else
    {
        TW *= _scale;
        TH *= _scale;
    }
    
    CGRect rect = CGRectMake(0, 0, TW, TH);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [_delegate addImgViewSelectImg:image];
}
@end
